/*
 * @author: Sawyer(870756077@qq.com)
 * @date: 2017-10-24 11:12:53
 * @last modified by:Sawyer(870756077@qq.com)
 * @last modified time: 2017-10-24 11:12:53
 */

import * as types from '../mutation-types'

let state = {
    /**
     * 限制当前可选个数
     */
    limit: 1,
    /**
     * 告诉picture modal 更改哪一个页面的某个属性
     */
    changeInfo: {},
    /**
     * goods create 页面，该页面存在该监听
     */
    goodsCreateStep1: {},
    /**
     * viusal 页面，该页面存在该监听
     */
    viusalModules: {}
}

let actions = {
    setPictureSel({ commit, rootState }, obj) {
        commit(types.SET_PICTURE_SEL, { ...obj, rootState })
    }
}

let mutations = {
    /**
     * 获取选中的图片根据其来源，赋值对应的对象
     *
     * @param {Array.<Object>} aPictureSel - 当前选中的图片合集
     */
    [types.SET_PICTURE_SEL](state, { aPictureSel, rootState }) {
        let changeInfo = state.changeInfo
        let pictureSel = JSON.parse(JSON.stringify(aPictureSel)) // 深拷贝
        if (changeInfo.page === 'goods/create') {
            let { attr, specIndex, tagIndex } = changeInfo
            state.goodsCreateStep1 = {
                attr,
                specIndex,
                tagIndex,
                aPictureSel: pictureSel
            }
        }
        if (changeInfo.page === 'viusal') {
            // 如果是可视化模块 直接在此处进行相关操作
            let modules = rootState.viusal.modules
            let { attr, type, modulesIndex, listIndex } = changeInfo
            if (type === 'updateImage') {
                switch (attr) {
                    case 'list':
                        modules[modulesIndex].data[attr][listIndex].img =
                            aPictureSel[0].img
                        break

                    default:
                        modules[modulesIndex].data[attr][listIndex].img =
                            aPictureSel[0].img
                        break
                }
            }
            if (type === 'addList') {
                aPictureSel.forEach(item => {
                    modules[modulesIndex].data[attr].push({
                        img: item.img,
                        title: '',
                        url: '',
                        urlCategory: '',
                        urlName: ''
                    })
                })
            }
        }
    }
}

export default {
    namespaced: true,
    state,
    actions,
    mutations
}
